package com.xy.smartsms.presenter;

import android.app.Activity;
import android.os.Handler;

import com.xy.smartsms.data.PublicInfoData;
import com.xy.smartsms.facade.SmsProcessFacade;
import com.xy.smartsms.util.Log;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by admin on 2017/1/13.
 */

public class ComposePresenterImpl extends ComposePresenter {
    private static final String TAG = ComposePresenterImpl.class.getSimpleName();
    private Handler mHandler = null;

    public ComposePresenterImpl(Activity context) {
        super(context);
        Log.i(TAG, "ComposePresenterImpl: ");
        mHandler = new Handler(context.getMainLooper());
    }

    public void finalize() throws Throwable {
        super.finalize();
        Log.i(TAG, "finalize: ");
    }

    @Override
    public void clearCache() {
        BubblePresenter.clearCache();
        //DuoquBubbleViewManager.clearCacheData(mContext);
        if (SmsProcessFacade.getInstance() != null) {
            SmsProcessFacade.getInstance().clearCache(mRecipientNumber);
        }
    }

    @Override
    public void preloadBubble() {
        Log.i(TAG, "preloadBubble: ");
        if (SmsProcessFacade.getInstance() != null) {
            SmsProcessFacade.getInstance().loadCache(mRecipientNumber);
        }
    }

    @Override
    public void loadMenu() {
        if (SmsProcessFacade.getInstance() == null) {
            return;
        }

        Map<String, String> extend = new HashMap<>();
        extend.put("iccid", "");
        SmsProcessFacade.getInstance().parsePublicInfo(mRecipientNumber,
                extend,
                new SmsProcessFacade.ISmsProcessPublicInfoCallback() {
                    @Override
                    public void onPublicInfo(String phone, final PublicInfoData publicInfoData) {
                        Log.i(TAG, "run: loadMenu:" + phone);
                        mHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (vista != null) {
                                    vista.showMenu(publicInfoData);
                                }
                            }
                        });
                    }
                });
    }

    @Override
    public void processMenuAction(JSONObject json) {
        try {
            Map<String, String> extend = new HashMap<String, String>();
            extend.put("simIndex", String.valueOf(-1));
            extend.put("phoneNum", mRecipientNumber);
            extend.put("ManufactureData", json.optString("ManufactureData"));
            SmsProcessFacade.getInstance().processAction(mContext, json.toString(), extend);
        } catch (Throwable t) {
            Log.e(TAG, "processMenuAction", t);
        }
    }
}
